package cn.tedu.springbootmybatis.controller;

import cn.tedu.springbootmybatis.pojo.dto.AdminAddNewDTO;
import cn.tedu.springbootmybatis.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.springbootmybatis.pojo.vo.AdminInfoVo;
import cn.tedu.springbootmybatis.restful.JsonResult;
import cn.tedu.springbootmybatis.restful.ResponseCode;

import cn.tedu.springbootmybatis.service.UserService;
import com.alibaba.druid.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: jiawp
 * @date: 2023/7/26 10:39
 * @description:
 **/
@RestController
@RequestMapping("/userManager")
@Api(tags = "用户管理模块")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("登录接口")
    @ApiImplicitParams(
            {@ApiImplicitParam(value = "用户名", name = "username",example = "admin", required = true),
             @ApiImplicitParam(value = "密码", name = "password",example = "admin", required = true)}
    )
    public JsonResult login(@RequestBody AdminLoginInfoDTO adminLoginInfoDTO) {
        if (StringUtils.isEmpty(adminLoginInfoDTO.getUsername()) || StringUtils.isEmpty(adminLoginInfoDTO.getPassword())) {
            return JsonResult.failed(ResponseCode.BAD_REQUEST, "请输入用户名和密码");
        } else {
            //查询数据库用户名 密码是否正确 正确 即 登录成功，错误抛出异常
            String password = userService.getPassword(adminLoginInfoDTO.getUsername());
            if (password == null) {
                return JsonResult.failed(ResponseCode.CONFLICT, "用户名错误");
            } else if (adminLoginInfoDTO.getPassword().equals(password)) {
                return JsonResult.ok("登录成功");
            } else {
                return JsonResult.failed(ResponseCode.UNAUTHORIZED, "登录失败");
            }
        }
    }

    /**
     * 获取全部用户列表
     * @return
     */
    @RequestMapping("/searchUser")
    @ApiOperation("获取全部用户列表")
    public JsonResult<List<AdminInfoVo>> getUserList(){
        List<AdminInfoVo> userList = userService.getUserList();
        return JsonResult.ok("获取全部用户列表成功",userList);
    }

    /**
     * 注册用户
     * @param adminAddNewDTO
     * @return
     */
    @PostMapping("/register")
    public JsonResult<String> register(@RequestBody AdminAddNewDTO adminAddNewDTO){
        return JsonResult.ok(userService.register(adminAddNewDTO));
    }

    /**
     * 修改用户
     * @param adminAddNewDTO
     * @return
     */
    @PostMapping("/updateUser")
    public JsonResult<String> updateUser(@RequestBody AdminAddNewDTO adminAddNewDTO){
        return JsonResult.ok(userService.updateUser(adminAddNewDTO));
    }

}
